{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "6B 体积（模型大小 14.3 G，macbook pro 似乎跑不动）：\n",
    "\n",
    "- Salesforce 模型：https://huggingface.co/Salesforce/codegen-6B-mono\n",
    "- CarpserAI： https://huggingface.co/CarperAI/diff-codegen-6b-v2\n",
    "\n",
    "2B 模型（模型大小 5.7 G，macbook pro 似乎跑不动）：：\n",
    "\n",
    "- Salesforce 模型：https://huggingface.co/Salesforce/codegen-2B-mono\n",
    "- CarpserAI： https://huggingface.co/CarperAI/diff-codegen-2b-v2\n",
    "\n",
    "350M（模型大小 784 M，macbook pro 可以跑）：\n",
    "\n",
    "- Salesforce 模型：https://huggingface.co/Salesforce/codegen-350M-mono\n",
    "- CarpserAI 模型：https://huggingface.co/CarperAI/diff-codegen-350M-v2\n",
    "\n",
    "我看到的最好的是 Salesforce 模型，但是它的体积太大了，所以我改用 CarpserAI 的模型，基于 Salesforce 做了优化。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://testpypi.python.org/pypi\r\n",
      "Collecting transformers\r\n",
      "  Downloading https://test-files.pythonhosted.org/packages/18/18/2dab068a2f5dee0d0b97a5c51b57243884aff0c1d7381c4febea27dc9ccc/transformers-4.26.1.2-py3-none-any.whl (6.3 MB)\r\n",
      "\u001B[K     |████████████████████████████████| 6.3 MB 50 kB/s eta 0:00:012\r\n",
      "\u001B[?25hRequirement already satisfied: requests in /usr/local/anaconda3/lib/python3.9/site-packages (from transformers) (2.26.0)\r\n",
      "Requirement already satisfied: regex!=2019.12.17 in /usr/local/anaconda3/lib/python3.9/site-packages (from transformers) (2021.8.3)\r\n",
      "Requirement already satisfied: filelock in /usr/local/anaconda3/lib/python3.9/site-packages (from transformers) (3.3.1)\r\n",
      "\u001B[31mERROR: Could not find a version that satisfies the requirement huggingface-hub<1.0,>=0.11.0 (from transformers) (from versions: none)\u001B[0m\r\n",
      "\u001B[31mERROR: No matching distribution found for huggingface-hub<1.0,>=0.11.0\u001B[0m\r\n"
     ]
    }
   ],
   "source": [
    "## 安装 Transformers\n",
    "\n",
    "!pip install -i https://testpypi.python.org/pypi transformers"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\r\n",
      "  File \"<string>\", line 1, in <module>\r\n",
      "ModuleNotFoundError: No module named 'transformers'\r\n",
      "Traceback (most recent call last):\r\n",
      "  File \"<string>\", line 1, in <module>\r\n",
      "ModuleNotFoundError: No module named 'transformers'\r\n"
     ]
    }
   ],
   "source": [
    "# 测试安装\n",
    "\n",
    "!python -c \"from transformers import pipeline; classifier = pipeline('text-classification'); print(classifier('What a nice release'))\"\n",
    "!python -c \"from transformers import *\""
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def hello_world() {\n",
      "    return \"Hello World!\"\n",
      "\n",
      "def hello_world_with_params(name, age, gender):\n",
      "    return f\"Hello {name} you are {age} years old and you are {gender}.\"\n",
      "\n",
      "def hello_world_with_params_and_return(name, age, gender):\n",
      "    return f\"Hello {name} you are {age} years old and you are {gender}.\"\n",
      "\n",
      "def hello_world_with_params_and_return_and_return(name, age, gender):\n",
      "    return f\"Hello\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "# 可以直接从服务器端下载\n",
    "# tokenizer = AutoTokenizer.from_pretrained(\"Salesforce/codegen-6B-mono\")\n",
    "# model = AutoModelForCausalLM.from_pretrained(\"Salesforce/codegen-6B-mono\")\n",
    "\n",
    "# 或者用下载到本地的版本\n",
    "# git lfs install\n",
    "# git clone https://huggingface.co/Salesforce/codegen-350M-mono\n",
    "#\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"/Volumes/game/ai-research/codegen-350M-mono\")\n",
    "model = AutoModelForCausalLM.from_pretrained(\"/Volumes/game/ai-research/codegen-350M-mono\")\n",
    "\n",
    "text = \"def hello_world() {\"\n",
    "input_ids = tokenizer(text, return_tensors=\"pt\").input_ids\n",
    "\n",
    "generated_ids = model.generate(input_ids, max_length=128)\n",
    "print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def hello_world() {\n",
      "    return \"Hello, World!\";\n",
      "}\n",
      "\n",
      "\"\"\"\n",
      "\n",
      "# The function name must be the same as the function definition\n",
      "# The function body must be indented\n",
      "# The function name must be followed by a colon (:)\n",
      "# The function body must be indented\n",
      "# The function name must be followed by a colon (:)\n",
      "# The function body must be indented\n",
      "# The function name must be followed by a colon (:)\n",
      "# The function body must be indented\n",
      "# The function name must be followed by a colon (:)\n",
      "#\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"/Volumes/game/ai-research/codegen-2B-mono\")\n",
    "model = AutoModelForCausalLM.from_pretrained(\"/Volumes/game/ai-research/codegen-2B-mono\")\n",
    "\n",
    "text = \"def hello_world() {\"\n",
    "input_ids = tokenizer(text, return_tensors=\"pt\").input_ids\n",
    "\n",
    "generated_ids = model.generate(input_ids, max_length=128)\n",
    "print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
